// pages/courseDetail/courseDetail.js
var util = require('../../utils/util.js');
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showPhoneModal: false,//手机号绑定弹框
    businessactivityid:'',//课程详情
    businessInfo: {},//商家信息
    activityInfo: {},//活动信息
    inviteUserPhone:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options){
      console.log(options);
      var courseDetailId
      if (options.businessactivityid) {
        courseDetailId = options.businessactivityid;
        //console.log('222');
      }
      //来自分享的参数
      if (options.shareparam) {
        console.log('888');
        var info = JSON.parse(options.shareparam);
        courseDetailId = info.businessactivityid;
        var inviteUserPhone = info.level;
        getApp().globalData.invitePeopleNumber = inviteUserPhone;
        console.log(getApp().globalData.invitePeopleNumber);
      }
      this.setData({'businessactivityid': courseDetailId });
      this.initPageDetail(courseDetailId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  /**
 * 用户点击右上角分享
 */
  onShareAppMessage: function (res) {
    var head = this.data.activityInfo.activityname;
    var businessactivityid = this.data.businessactivityid;
    var imgUrl = this.data.businessInfo.businesspic;
    if (imgUrl) {
      imgUrl = this.data.businessInfo.businesspic
    } else {
      imgUrl = '/images/home_banner.png'
    }
    //var userInfo = getApp().globalData.userInfo
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    var inviteUserPhone = this.data.inviteUserPhone;
    console.log('000' + inviteUserPhone);
    var shareparam = JSON.stringify({ level: inviteUserPhone, businessactivityid: businessactivityid })
    return {
      title: head,
      path: "/pages/courseDetail/courseDetail?shareparam=" + shareparam,
      imageUrl: imgUrl
    }
  },
  //点击确定-bindPhone组件传过来的信息
  getBindInfo: function (e) {
    console.log(e);
    var bindInfo = e.detail.bindPhone;//true为手机绑定成功，false为手机绑定失败
    if (bindInfo) {
      var userInfo = e.detail.userInfo;
      console.log(userInfo);
      this.setData({ 'showPhoneModal': false });//绑定手机号成功后影藏弹框
    }
  },
  initPageDetail:function(courseId){
    var that = this;
    wx.getStorage({
      key: 'loginStutes',
      success: function(res) {
        var userInfo = JSON.parse(res.data);
        that.setData({ inviteUserPhone: userInfo.mobile });
      },
    })
    wx.request({
      url: getApp().apiUrl + '/api/businessactivity/info/' + courseId,
      method: 'post',
      header: { 'content-type': 'application/x-www-form-urlencoded'},
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          var hostInfo = res.data.business;//商家信息
          var activityInfo = res.data.businessactivity;//商家活动
          //console.log(activityInfo);
          that.setData({ 'businessInfo': hostInfo, 'activityInfo': activityInfo });
          //富文本
          var article = activityInfo.content;
          //article.replace(/\<img/gi, '<img style="max-width:100%;height:auto;width:100px;"')
          WxParse.wxParse('article', 'html', article, that, 15);
          // var data = res.data.page.list;
          // that.setData({ 'homeList': homeList });
        };

      },

    })
  },
  //跳转至确认订单页面
  skip_sureOrders:function(){
    //创建支付订单
    var that = this;
    var businessactivityid = that.data.businessactivityid;
    wx.getStorage({
      key: 'loginStutes',
      success: function (res) {
        //console.log(res);
        var userInfo = JSON.parse(res.data);
        //console.log(userInfo);
        var tokenVal = userInfo.app_token;
        wx.request({
          url: getApp().apiUrl + '/api/order/creatOrder',
          method: 'post',
          data: { 'ordertype':6, 'businessactivityid': businessactivityid },
          header: { 'content-type': 'application/x-www-form-urlencoded', 'Authorization': tokenVal },
          success: function (res) {
            console.log(res);
            if (res.data.code == 0) {
              var activityInfo = that.data.activityInfo;
              var orderInfo = res.data.order;
              if (orderInfo.orderpic){orderInfo.orderpic = encodeURIComponent(orderInfo.orderpic);}   
              if (orderInfo.qrcode){orderInfo.qrcode = encodeURIComponent(orderInfo.qrcode);}         
              console.log(orderInfo);
              orderInfo = JSON.stringify(res.data.order);
              wx.navigateTo({
                url: '/pages/sureOrders/sureOrders?orderInfo=' + orderInfo
              })//领取成功跳转到-确认订单页面
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none'
              })
            };

          },

        })
      },
      fail: function (res) {
        that.setData({'showPhoneModal': true });
      }
    })
  }
})